package com.skvalex.transport;

import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import com.actionbarsherlock.app.SherlockPreferenceActivity;

import java.util.Calendar;

/**
 * User: skvalex@gmail.com
 * Date: 11.05.12 18:17
 */
public class Settings extends SherlockPreferenceActivity {

    private static final String SHOW_BUS_FLAG = "vehicle_bus_pref";
    private static final String SHOW_TROLLEY_FLAG = "vehicle_trolley_pref";
    private static final String SHOW_TRAM_FLAG = "vehicle_tram_pref";
    private static final String MAP_TYPE_PREF = "map_type_pref";
    private static final String REFRESH_PERIOD_PREF = "refresh_period_pref";
    private static final String VERSION_PREF = "version_pref";
    private static final String COPYRIGHT_PREF = "copyright_pref";

    public static final int MAP_TYPE_STREET = 0;
    public static final int MAP_TYPE_SATELLITE = 1;

    private ListPreference mapTypePreference;
    private ListPreference refreshPeriodPreference;

    private PreferenceScreen versionPreference;
    private PreferenceScreen copyrightPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        mapTypePreference = (ListPreference) findPreference(MAP_TYPE_PREF);
        refreshPeriodPreference = (ListPreference) findPreference(REFRESH_PERIOD_PREF);
        versionPreference = (PreferenceScreen) findPreference(VERSION_PREF);
        copyrightPreference = (PreferenceScreen) findPreference(COPYRIGHT_PREF);

        mapTypePreference.setSummary(mapTypePreference.getEntries()[Settings.getMapType()]);
        mapTypePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference pref, Object newValue) {
                ListPreference preference = (ListPreference) pref;
                preference.setSummary(preference.getEntries()[Integer.parseInt(newValue.toString())]);
                return true;
            }
        });

        refreshPeriodPreference.setSummary(refreshPeriodPreference.getEntries()[Settings.getRefreshPeriodValue()]);
        refreshPeriodPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference pref, Object newValue) {
                ListPreference preference = (ListPreference) pref;
                preference.setSummary(preference.getEntries()[Integer.parseInt(newValue.toString())]);
                return true;
            }
        });

        try {
            versionPreference.setSummary(getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        copyrightPreference.setSummary(getCopyrightSummary());
    }

    private String getCopyrightSummary() {
        String interval = 2012 + "";
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        if (currentYear > 2012) {
            interval += "-" + currentYear;
        }
        return String.format(getString(R.string.copyright_summary), interval);
    }

    public static boolean isShowBusFlagEnabled() {
        SharedPreferences settingPref = PreferenceManager.getDefaultSharedPreferences(TransportApp.getContext());
        return settingPref.getBoolean(SHOW_BUS_FLAG, TransportApp.getContext().getResources().getBoolean(R.bool.vehicle_bus_default_pref));
    }

    public static boolean isShowTrolleyFlagEnabled() {
        SharedPreferences settingPref = PreferenceManager.getDefaultSharedPreferences(TransportApp.getContext());
        return settingPref.getBoolean(SHOW_TROLLEY_FLAG, TransportApp.getContext().getResources().getBoolean(R.bool.vehicle_trolley_default_pref));
    }

    public static boolean isShowTramFlagEnabled() {
        SharedPreferences settingPref = PreferenceManager.getDefaultSharedPreferences(TransportApp.getContext());
        return settingPref.getBoolean(SHOW_TRAM_FLAG, TransportApp.getContext().getResources().getBoolean(R.bool.vehicle_tram_default_pref));
    }

    public static int getMapType() {
        SharedPreferences settingPref = PreferenceManager.getDefaultSharedPreferences(TransportApp.getContext());
        return Integer.parseInt(settingPref.getString(MAP_TYPE_PREF, TransportApp.getContext().getResources().getString(R.string.map_type_default_pref)));
    }

    private static int getRefreshPeriodValue() {
        SharedPreferences settingPref = PreferenceManager.getDefaultSharedPreferences(TransportApp.getContext());
        return Integer.parseInt(settingPref.getString(REFRESH_PERIOD_PREF, TransportApp.getContext().getResources().getString(R.string.refresh_period_default_pref)));
    }

    public static int getDefaultZoom() {
        return 15;
    }

    public static int getRefreshPeriodInMs() {
        switch (Settings.getRefreshPeriodValue()) {
            case 0:
                return 5000;
            case 1:
                return 10000;
            case 2:
                return 15000;
            case 3:
                return 20000;
            case 4:
                return 30000;
            case 5:
                return 45000;
            case 6:
                return 60000;
        }
        return 45000;
    }
}
